
package com.akk.utils;

import com.akk.exception.ErrorResult;
import org.springframework.http.HttpStatus;
import java.io.Serializable;
import java.util.UUID;

/**
 * 描述 : rest响应对象
 *
 * @author wangkang
 */
public class RestResponse<T> implements Serializable {

    public static final String PLATFORM_ERROR_CODE="20001";
    public static final String AVS_ERROR_CODE="20002";
    public static final String HXT_TOKEN_OVERDUE="20003";
    /**
     * 描述 : id
     */
    private static final long serialVersionUID = 1L;

    /**
     * 描述 : 响应ID
     */
    private String id = UUID.randomUUID().toString();

    /**
     * 描述 : 状态码(业务定义)
     */
    private String code = Integer.toString(HttpStatus.OK.value());

    /**
     * 描述 : 状态码描述(业务定义)
     */
    private String message = HttpStatus.OK.getReasonPhrase();

    /**
     * 描述 : 结果集(泛型)
     */
    private T result = null; //NOSONAR

    /**
     * 描述 : 错误
     */
    private ErrorResult error = null;

    /**
     * 描述 : 构造函数
     */
    public RestResponse() {
        super();
    }

    /**
     * 描述 : 构造函数
     *
     * @param result 结果集(泛型)
     */
    public RestResponse(T result) {
        super();
        this.result = result;
    }

    /**
     * 描述 : 构造函数
     *
     * @param httpStatus http状态
     * @param error      错误
     */
    public RestResponse(HttpStatus httpStatus, ErrorResult error) {
        super();
        this.code = Integer.toString(httpStatus.value());
        this.message = httpStatus.getReasonPhrase();
        this.error = error;
    }

    /**
     * 描述 : 构造函数
     *
     * @param code    状态码(业务定义)
     * @param message 状态码描述(业务定义)
     * @param error   错误
     */
    public RestResponse(String code, String message, ErrorResult error) {
        super();
        this.code = code;
        this.message = message;
        this.error = error;
    }

    /**
     * 描述 : 构造函数
     *
     * @param code    状态码(业务定义)
     * @param message 状态码描述(业务定义)
     * @param result  结果集(泛型)
     */
    public RestResponse(String code, String message, T result) {
        super();
        this.code = code;
        this.message = message;
        this.result = result;
    }

    @SuppressWarnings("unchecked")
    public static RestResponse build(String code, String message, Object result){
        RestResponse response = new RestResponse(code,message,result);
        return response;
    }
    @SuppressWarnings("unchecked")
    public static RestResponse build(Object result){
        RestResponse response = new RestResponse(result);
        return response;
    }

    public static RestResponse build(String code,String message){
        return RestResponse.build(code,message,null);
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getResult() {
        return result;
    }

    public void setResult(T result) {
        this.result = result;
    }

    public ErrorResult getError() {
        return error;
    }

    public void setError(ErrorResult error) {
        this.error = error;
    }
}
